package com.tjetc;

import com.tjetc.dao.BankDao;
import com.tjetc.pojo.Bank;
import com.tjetc.utils.MyBatisUtil;
import org.apache.ibatis.executor.BaseExecutor;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;

/**
 * @author gaoyongxiang
 * @version v1.0
 * @date 2025/8/20
 */
public class _6Cache {

    @Test
    public void testCache(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        BankDao bankDao = sqlSession.getMapper(BankDao.class);
        Bank bank = bankDao.selectBank(1);
        System.out.println(bank);

        // Mybatis默认开启一级缓存 在查询的时候 如果出现相同的select语句 就不会走数据库了
        Bank bank1 = bankDao.selectBank(1);
        System.out.println(bank1);
    }

    @Test
    public void testCacheFailure(){
        /*
            缓存失效：
            1. 执行过clearCache()
            2. 数据发生变化 执行修改

            MyBatis本身可以对cache做一些相关的配置
         */
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        BankDao bankDao = sqlSession.getMapper(BankDao.class);
        Bank bank = bankDao.selectBank(1);
        System.out.println(bank);
        // 调用清空缓存
//        sqlSession.clearCache();
        // 更新数据
        bank.setId(2);
        bank.setName("zhang");
        bankDao.updateBank(bank);
        Bank bank1 = bankDao.selectBank(1);
        System.out.println(bank1);
    }
}